package beans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;

import javax.persistence.criteria.Root;

import model.Proprietate;
import model.Teren;
import model.Teren_;

/**
 * Session Bean implementation class UtilStateless
 */
@Stateless
public class UtilStateless implements UtilStatelessRemote {

	@PersistenceContext
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public UtilStateless() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void showProprieties() {

		List<Proprietate> props = em.createNamedQuery("Proprietate.findAll",
				Proprietate.class).getResultList();

		for (Proprietate p : props) {
			System.out.println("ID:" + p.getId() + "  Adresa:" + p.getAdresa());
		}
	}

	

	

	

	public void getTeren(int pret) {

		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Teren> cq = cb.createQuery(Teren.class);
		Root<Teren> teren = cq.from(Teren.class);

		Expression<Integer> p = cb.prod(teren.get(Teren_.suprafata),
				teren.get(Teren_.pretUnitar));
		// cq.select(casa).where(cb.ge(casa.get(Casa_.pret),pret));

		cq.where(cb.greaterThan(p, pret));

		TypedQuery<Teren> q = em.createQuery(cq);
		List<Teren> casee = q.getResultList();

		for (Teren c : casee) {
			System.out.println(c.getAdresa());
		}

	}

	

	

}
